package com.nov.multidatasource.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.nov.multidatasource.holder.ThreadLocalHolder;
import com.nov.multidatasource.service.RunBatchService;
import com.tecrun.common.vo.RtnData;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @ClassName RunBatchController
 * @Description 跑批 controller
 * @Author cxn
 * @CreateTime 2024/12/24 14:40
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/run/batch")
@Slf4j
public class RunBatchController {

    @Resource
    private RunBatchService runBatchService;

    /**
     * 在共享中心里面那些数据是同步过的, 并且minio的文件也是有的，
     * 在攀钢的文件表里面打个标
     */
    @RequestMapping(value = "/data/markSyncDone")
    public RtnData<String> markSyncDone(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            runBatchService.markSyncDone();
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
        return RtnData.success("success");
    }


    @RequestMapping(value = "/runFailTxt")
    public RtnData<String> runFailTxt(HttpServletRequest request) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            String path = request.getParameter("path");
            return RtnData.success(runBatchService.runFailTxtFile(path));
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
    }

    @RequestMapping(value = "/syncPanGangMinio")
    public RtnData<String> syncPanGangMinio(HttpServletRequest request) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            return RtnData.success(runBatchService.syncPanGangMinio());
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
    }


    @RequestMapping(value = "/getAnGangShareMinioUrl")
    public RtnData<String> getAnGangShareMinioUrl(HttpServletRequest request) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            String fileId = request.getParameter("fileId");
            if(StrUtil.isEmpty(fileId)){
                log.info("fileId, 参数不存在");
            }
            return RtnData.success(runBatchService.getAnGangShareMinioUrl(fileId));
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
    }



    @RequestMapping(value = "/getPanGangMinioUrl")
    public RtnData<String> getPanGangMinioUrl(HttpServletRequest request) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            String fileId = request.getParameter("fileId");
            if(StrUtil.isEmpty(fileId)){
                log.info("fileId, 参数不存在");
            }
            return RtnData.success(runBatchService.getPanGangMinioUrl(fileId));
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
    }


    @RequestMapping(value = "/syncTestPanGangFile")
    public RtnData syncTestPanGangFile(HttpServletRequest request) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            String fileId = request.getParameter("fileId");
            if(StrUtil.isEmpty(fileId)){
                log.info("fileId, 参数不存在");
            }
            runBatchService.syncTestPanGangFile(fileId);
            return (RtnData) RtnData.success();
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
    }

    @RequestMapping(value = "/fixFilePathIndex")
    public RtnData fixFilePathIndex(HttpServletRequest request) throws Exception {
        MDC.put("trace_id", "schedule-run-batch-" + LocalDateTime.now());
        try {
            runBatchService.fixFilePathIndex();
            return (RtnData) RtnData.success();
        } catch (Exception e) {
            log.error("数据迁移异常", e);
            return RtnData.success("FAIL");
        } finally {
            MDC.remove("trace_id");
            ThreadLocalHolder.removeAll();
        }
    }

}

